//
//  TZVisitorView.swift
//  weibo_Swift
//
//  Created by 涂真 on 15/12/16.
//  Copyright © 2015年 涂真. All rights reserved.
//

import UIKit
//MARK: - 定义协议
protocol TZVisitorViewDelegate:NSObjectProtocol{
    func loginBtnDidClick()
    func registerBtnDidClick()
}

class TZVisitorView: UIView {
    
    ///代理属性
    weak var delegate : TZVisitorViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        settingVisitorView()
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
    }
    
    //MARK:添加游客视图
    func settingVisitorView(){
        addSubview(circleView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(hintLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        circleView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        hintLable.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束
        //添加外圈约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
        //添加房子约束
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //添加提示的约束
        addConstraint(NSLayoutConstraint(item: hintLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: hintLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: hintLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        //添加登陆按钮约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: hintLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: hintLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //添加注册按钮约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: hintLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: hintLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //添加遮罩层的约束
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //底部
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -190))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        
    }
    
    //MARK:旋转外圈
    func rotateCircleView(){
       let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        
        circleView.layer.addAnimation(animation, forKey: nil)
        
    }
    ///后台停止
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = circleView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        circleView.layer.speed = 0
        
        // 设置动画偏移时间
        circleView.layer.timeOffset = pauseTime
    }
    ///前台开启
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = circleView.layer.timeOffset
        
        // 设置动画速度为1
        circleView.layer.speed = 1
        
        // 重置偏移时间
        circleView.layer.timeOffset = 0
        
        // 重置开始时间
        circleView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = circleView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        circleView.layer.beginTime = timeSincePause
    }
    
    ///更新视图
    func setupVisitorView(imageName:String,hintText:String){
        coverView.hidden = true
        homeView.hidden = true
        circleView.image = UIImage(named: imageName)
        hintLable.text = hintText
        
    }
    ///登录功能
    func login(){
    delegate?.loginBtnDidClick()
    }
    ///注册功能
    @objc private func register(){
    delegate?.registerBtnDidClick()
    }
    
    //MARK:懒加载控件
    //外圈
    private lazy var circleView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
    private lazy var homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //标语
    private lazy var hintLable:UILabel = {
        let lable = UILabel()
        lable.text = "关注一些人,看看这里有什么惊喜!"
        lable.textColor = UIColor.darkGrayColor()
        lable.numberOfLines = 0
        return lable
    }()
    //登陆按钮
    private lazy var loginBtn:UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    //注册按钮
    private lazy var registerBtn:UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    //遮罩层
    private lazy var coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
}
